from flask import Blueprint,request,jsonify
from db import db1
from comm import get_data
bp_news = Blueprint('news',__name__)

@bp_news.route('/domesticM',methods=['GET','POST'])
def domesticM():
    if request.method == 'GET':
        sql = "select id,name from domestic"
        rse = db1.find_all(sql)
        return jsonify({'code':200,'list':rse})
    else:
        data = get_data()
        sql = "insert into domestic values(0,'%s')"%(data['name'])
        db1.insert(sql)
        return jsonify({'code':200})

@bp_news.route('/gameM',methods=['GET','POST'])
def gameM():
    if request.method == 'GET':
        id = request.args.get('id')
        if not id:
            sql = "select id,title,dom_id from game where dom_id=(select id from domestic)"
            rse = db1.find_all(sql)[:2]
            return jsonify({'code':200,'list':rse})
        else:
            page = int(request.args.get('page'))
            page_size = int(request.args.get('page_size'))
            start = (int(page)-1)*page_size
            sql = "select id,title,dom_id from game limit %d,%d"%(start,page_size)
            sql1 = "select * from game where dom_id=(select id from domestic)"
            rse = db1.find_all(sql)
            return jsonify({'code':200,'list':rse})
    else:
        data = get_data()
        sql = "insert into game values(0,'%s',%d)"%(data['title'],data['dom_id'])
        db1.insert(sql)
        return jsonify({'code':200})